<?php
namespace app\api\controller;
use app\common\controller\Base;
use Pay\Pay;
use think\Db;
/**
 * 支付回调
 */
class Notify extends Base{

    /**
     * [alipay 支付宝支付回调]
     * @return [type] [description]
     */
    public function alipay(){
        $pay = new Pay(config('pay'));
        $notifyInfo = $pay->driver('alipay')->gateway('app')->verify($this->post);
        if(in_array($notifyInfo['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
            $attach = unserialize(UrlDecode($notifyInfo['passback_params']));  //自定义参数
            $transaction_id = $notifyInfo['trade_no']; //支付宝交易号
            $out_trade_no = $notifyInfo['out_trade_no']; //商户订单号
            $total_fee = $notifyInfo['total_amount']; //支付金额
            $pay_time = isset($notifyInfo['gmt_payment']) ? strtotime($notifyInfo['gmt_payment']) : time(); //支付时间

            //发红包
            if($attach['attach'] == 1){

                //红包详情
                $red_info = model('common/red_packet')->where('order_no',$out_trade_no)->find();

                db()->startTrans();
                
                //修改红包状态
                if(!$red_info->save([
                    'pay_time' => $pay_time,
                    'pay_status' => 1,
                    'tran_no' => $transaction_id,
                ])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                //交易明细
                if(!db('bill')->insert([
                    'user_id' => $red_info['user_id'],
                    'type' => 1,
                    'num' => $red_info['price'],
                    'foreign_key' => $red_info['id'],
                    'add_time' => time()
                ])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }
         
                //消息通知
                if(!db('information')->insert([
                    'user_id' => $red_info['user_id'],
                    'type' => 1,
                    'content' => '红包支付成功',
                    'add_time' => time()
                ])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }
               
                db()->commit();

                echo "success"; 
                die;
            }

            //需求-定金支付
            if($attach['attach'] == 2){

                $need_id = $attach['id'];  //需求id

                $apply_user = $attach['apply_user'];  //申请人id
                //需求详情
                $need_info = db('need')->where('id',$need_id)->find();
                //申请详情
                $apply_info = model('common/need_apply')->where('apply_user',$apply_user)->where('foreign_key',$need_id)->find();

                //申请人信息
                $applyer = model('common/user')->getUserInfo(['id' => $apply_user],'grade_end_time,grade,from_city');

                //佣金
                if($applyer['grade'] == 0){
                    $yongjin = db('web_config')->where('key','p_yongjin')->value('value');
                }else{ //会员
                    $yongjin = db('web_config')->where('key','h_yongjin')->value('value');
                }

                $applyer_price = sprintf('%.2f',$need_info['pay_price']-$yongjin); //申请者所得金额

                db()->startTrans();

                if(!db('user')->where('id',$apply_user)->setInc('balance',$applyer_price)){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                $bill_count = db('bill')->insertAll([
                    [
                        'user_id' => $need_info['user_id'],
                        'type' => 3,
                        'num' => $total_fee,
                        'foreign_key' => $apply_info['id'],
                        'add_time' => time()
                    ],[
                        'user_id' => $apply_user,
                        'type' => 4,
                        'num' => $applyer_price,
                        'foreign_key' => $apply_info['id'],
                        'add_time' => time()
                    ]
                ]);

                if(!$bill_count){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                 //信息
                $infor_count = db('information')->insertAll([
                    [
                        'user_id' => $need_info['user_id'],
                        'type' => 3,
                        'content' => '需求-'.$need_info['title'].'支付成功',
                        'add_time' => time()
                    ],[
                        'user_id' => $apply_user,
                        'type' => 4,
                        'content' => '需求-'.$need_info['title'].'申请通过',
                        'add_time' => time()
                    ]
                ]);

                if(!$infor_count){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                if(!$apply_info->save([
                    'price' => $applyer_price,
                    'order_no' => $out_trade_no,
                    'tran_no' => $transaction_id,
                    'status' => 1,
                    'pay_type' => 2,
                    'is_checked' => 1,
                    'pay_time' => $pay_time
                ])){   
                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                if(!model('common/admin_bill')->addBill($applyer['from_city'],$yongjin,$apply_info['id'],1)){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }
                
                db()->commit();

                echo "success"; 
                die;
            }

            //购买广告位
            if($attach['attach'] == 3){
                $model = model('common/ad_buy')->get($attach['id']);

                db()->startTrans();

                if(!$model->save([
                    'order_no' => $out_trade_no,
                    'tran_no' => $transaction_id,
                    'status' => 1,
                    'pay_type' => 2,
                    'pay_time' => $pay_time,
                ])){

                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                if(!db('bill')->insert([
                    'user_id' => $model['user_id'],
                    'type' => 5,
                    'num' => $model['price'],
                    'foreign_key' => $model['id'],
                    'add_time' => time()
                ])){

                    db()->rollback();
                    echo "fail"; 
                    die;

                }

                if(!db('information')->insert([
                    'user_id' => $model['user_id'],
                    'type' => 5,
                    'content' => '购买广告位，支付成功',
                    'add_time' => time()
                ])){

                    db()->rollback();
                    echo "fail"; 
                    die;
                }
               
                $user_info = db('user')->where('id',$model['user_id'])->find();

                if(!model('common/admin_bill')->addBill($user_info['from_city'],$model['price'],$model['id'],2)){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }
                
                db()->commit();
                echo "success"; 
                die;
            }
            
            //购买服务
            if($attach['attach'] == 4){
                //订单详情
                $info = model('common/service_buy')->get($attach['id']);

                //服务详情
                $service_info = db('service')->where('id',$info['foreign_key'])->find();

                db()->startTrans();
                if(!$info->save([
                    'order_no' => $out_trade_no,
                    'tran_no' => $transaction_id,
                    'pay_type' => 2,
                    'pay_time' => $pay_time,
                    'status' => 1
                ])){

                    db()->rollback();
                    echo "fail"; 
                    die;
                }
                //售卖者信息
                $service_user = model('common/user')->getUserInfo(['id' => $info['issue_id']],'grade,grade_end_time,from_city');

                //佣金
                if($service_user['grade'] == 1){
                    $yongjin = db('web_config')->where('key','h_yongjin')->value('value');
                }else{
                    $yongjin = db('web_config')->where('key','p_yongjin')->value('value');
                }

                $seller_price = sprintf('%.2f',$info['price'] - $yongjin);  //售卖者所得金额

                if(!db('user')->where('id',$info['issue_id'])->update([
                    'balance' => ['inc',$seller_price],
                    'service_count' => ['inc',1]
                ])){

                    db()->rollback();
                    echo "fail"; 
                    die;
                   
                }

                if(!db('bill')->insertAll([
                    [
                        'user_id' => $info['buy_user'],
                        'type' => 6,
                        'num' => $info['price'],
                        'foreign_key' => $info['id'],
                        'add_time' => time()
                    ],[
                        'user_id' => $info['issue_id'],
                        'type' => 8,
                        'num' => $seller_price,
                        'foreign_key' => $info['id'],
                        'add_time' => time()
                    ]
                ])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                if(!db('information')->insertAll([
                    [
                        'user_id' => $info['buy_user'],
                        'type' => 6,
                        'content' => '服务-'.$service_info['title'].'购买成功',
                        'add_time' => time()
                    ],[
                        'user_id' => $info['issue_id'],
                        'type' => 8,
                        'content' => '服务-'.$service_info['title'].'被购买',
                        'add_time' => time()
                    ]
                ])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                if(!model('common/admin_bill')->addBill($service_user['from_city'],$yongjin,$info['id'],3)){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                db()->commit();

                echo "success"; 
                die;
            }

            //服务打赏
            if($attach['attach'] == 5){     
                //订单详情
                $model = model('common/service_buy')->get($attach['id']);

                //服务详情
                $service_info = db('service')->where('id',$model['foreign_key'])->find();

                db()->startTrans();

                if(!db('user')->where('id',$model['issue_id'])->setInc('balance',$total_fee)){

                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                if(!db('bill')->insertAll([[
                        'user_id' => $model['buy_user'],
                        'type' => 9,
                        'num' => $total_fee,
                        'foreign_key' => $model['id'],
                        'add_time' => time()
                    ],[
                        'user_id' => $model['issue_id'],
                        'type' => 10,
                        'num' => $total_fee,
                        'foreign_key' => $model['id'],
                        'add_time' => time()
                    ]])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                if(!db('information')->insertAll([[
                        'user_id' => $model['buy_user'],
                        'type' => 9,
                        'content' => '服务-'.$service_info['title'].'打赏成功',
                        'add_time' => time()
                    ],[
                        'user_id' => $model['issue_id'],
                        'type' => 10,
                        'content' => '服务-'.$service_info['title'].'被打赏'.$total_fee.'元',
                        'add_time' => time()
                    ]])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                if(!$model->save([
                    'dashang' => 1,
                    'ds_price' => $total_fee,
                    'ds_pay_type' => 2,
                    'ds_order_no' => $out_trade_no,
                    'ds_tran_no' => $transaction_id,
                    'ds_pay_time' => $pay_time
                ])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                db()->commit();

                echo "success"; 
                die;

            }

            //会员升级
            if($attach['attach'] == 6){
                //会员详情
                $user_info = model('common/user')->getUserInfo(['id' => $attach['user_id']],'grade,grade_end_time');
                //会员价格详情
                $res = db('grade')->where('id',$attach['type'])->find();
                if($user_info['grade'] == 1){
                    $grade_end_time = strtotime($user_info['grade_end_time']);
                }else{
                    $grade_end_time = strtotime(date('Ymd'));
                }
                switch($attach['type']){
                    case 1: $grade_end_time = strtotime('+1 month',$grade_end_time); break;  //包月
                    case 2: $grade_end_time = strtotime('+6 month',$grade_end_time); break; //半年
                    case 3: $grade_end_time = strtotime('+1 years',$grade_end_time); break; //包年
                }
                db()->startTrans();
                //修改会员信息
                if(!db('user')->where('id',$attach['user_id'])->update([
                    'grade_end_time' => $grade_end_time,
                    'grade' => 1
                ])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }
                $insert = db('upgrade')->insertGetId([
                    'user_id' => $attach['user_id'],
                    'grade_type' => $attach['type'],
                    'price' => $total_fee,
                    'pay_type' => 2,
                    'order_no' => $out_trade_no,
                    'tran_no' => $transaction_id,
                    'pay_time' => $pay_time
                ]);

                //购买记录
                if(!$insert){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }
                if(!db('bill')->insert([
                    'user_id' => $attach['user_id'],
                    'type' => 11,
                    'num' => $total_fee,
                    'foreign_key' => $insert,
                    'add_time' => time()
                ])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                if(!db('information')->insert([
                    'user_id' => $attach['user_id'] ,
                    'type' => 11 ,
                    'content' => '购买会员-支付成功',
                    'add_time' => time()
                ])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }
                db()->commit();
                echo "success"; 
                die;

            }

            //购买定位
            if($attach['attach'] == 7){
                db()->startTrans();
                //购买记录
                if(!$id = db('position_buy')->insertGetId([
                    'user_id' => $attach['user'],
                    'num' => $attach['num'],
                    'price' => $total_fee,
                    'order_no' => $out_trade_no,
                    'tran_no' => $transaction_id,
                    'pay_type' => 2,
                    'pay_status' => 1,
                    'pay_time' => $pay_time
                ])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }
                
                //添加交易明细
                if(!db('bill')->insert([
                    'user_id' => $attach['user'],
                    'type' => 13,
                    'num' => $total_fee,
                    'foreign_key' => $id,
                    'add_time' => time()
                ])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }

                //消息通知
                if(!db('information')->insert([
                    'user_id' => $attach['user'],
                    'type' => 13,
                    'content' => '购买定位-支付成功',
                    'add_time' => time()
                ])){
                    db()->rollback();
                    echo "fail"; 
                    die;
                }
                db()->commit();
                echo "success"; 
                die;
            }
        }
        echo "fail"; //验证失败
        die;
    }

    /**
     * [wechat 微信支付回调]
     * @return [type] [description]
     */
    public function wechat(){
        $pay = new Pay(config('pay'));
        $notifyInfo = $pay->driver('wechat')->gateway('mp')->verify(file_get_contents('php://input'));
        if ($notifyInfo['result_code'] == 'SUCCESS' && $notifyInfo['return_code'] == 'SUCCESS') {

            $attach = unserialize($notifyInfo['attach']);  //自定义参数
            $transaction_id = $notifyInfo['transaction_id']; //微信订单号
            $out_trade_no = $notifyInfo['out_trade_no']; //商户订单号
            $total_fee = $notifyInfo['total_fee']/100; //支付金额
            $pay_time = isset($notifyInfo['time_end']) ? strtotime($notifyInfo['time_end']) : time(); //支付时间
         

            //发红包
            if($attach['attach'] == 1){

                //红包详情
                $red_info = model('common/red_packet')->where('order_no',$out_trade_no)->find();

                db()->startTrans();
                
                //修改红包状态
                if(!$red_info->save([
                    'pay_time' => $pay_time,
                    'pay_status' => 1,
                    'tran_no' => $transaction_id,
                ])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                //交易明细
                if(!db('bill')->insert([
                    'user_id' => $red_info['user_id'],
                    'type' => 1,
                    'num' => $red_info['price'],
                    'foreign_key' => $red_info['id'],
                    'add_time' => time()
                ])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }
         
                //消息通知
                if(!db('information')->insert([
                    'user_id' => $red_info['user_id'],
                    'type' => 1,
                    'content' => '红包支付成功',
                    'add_time' => time()
                ])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }
               
                db()->commit();

                echo $this->arrayToXml(['return_code' => 'SUCCESS','return_msg' => '回调成功']);die;
            }

            //需求-定金支付
            if($attach['attach'] == 2){

                $need_id = $attach['id'];  //需求id

                $apply_user = $attach['apply_user'];  //申请人id
                //需求详情
                $need_info = db('need')->where('id',$need_id)->find();
                //申请详情
                $apply_info = model('common/need_apply')->where('apply_user',$apply_user)->where('foreign_key',$need_id)->find();

                //申请人信息
                $applyer = model('common/user')->getUserInfo(['id' => $apply_user],'grade_end_time,grade,from_city');

                //佣金
                if($applyer['grade'] == 0){
                    $yongjin = db('web_config')->where('key','p_yongjin')->value('value');
                }else{ //会员
                    $yongjin = db('web_config')->where('key','h_yongjin')->value('value');
                }

                $applyer_price = sprintf('%.2f',$need_info['pay_price']-$yongjin); //申请者所得金额

                db()->startTrans();

                if(!db('user')->where('id',$apply_user)->setInc('balance',$applyer_price)){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die; 
                }

                $bill_count = db('bill')->insertAll([
                    [
                        'user_id' => $need_info['user_id'],
                        'type' => 3,
                        'num' => $total_fee,
                        'foreign_key' => $apply_info['id'],
                        'add_time' => time()
                    ],[
                        'user_id' => $apply_user,
                        'type' => 4,
                        'num' => $applyer_price,
                        'foreign_key' => $apply_info['id'],
                        'add_time' => time()
                    ]
                ]);

                if(!$bill_count){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                 //信息
                $infor_count = db('information')->insertAll([
                    [
                        'user_id' => $need_info['user_id'],
                        'type' => 3,
                        'content' => '需求-'.$need_info['title'].'支付成功',
                        'add_time' => time()
                    ],[
                        'user_id' => $apply_user,
                        'type' => 4,
                        'content' => '需求-'.$need_info['title'].'申请通过',
                        'add_time' => time()
                    ]
                ]);

                if(!$infor_count){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                if(!$apply_info->save([
                    'price' => $applyer_price,
                    'order_no' => $out_trade_no,
                    'tran_no' => $transaction_id,
                    'status' => 1,
                    'pay_type' => 1,
                    'is_checked' => 1,
                    'pay_time' => $pay_time
                ])){   
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                if(!model('common/admin_bill')->addBill($applyer['from_city'],$yongjin,$apply_info['id'],1)){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }
                
                db()->commit();

                echo $this->arrayToXml(['return_code' => 'SUCCESS','return_msg' => '回调成功']);die;
            }

            //购买广告位
            if($attach['attach'] == 3){
                $model = model('common/ad_buy')->get($attach['id']);

                db()->startTrans();

                if(!$model->save([
                    'order_no' => $out_trade_no,
                    'tran_no' => $transaction_id,
                    'status' => 1,
                    'pay_type' => 1,
                    'pay_time' => $pay_time,
                ])){

                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                if(!db('bill')->insert([
                    'user_id' => $model['user_id'],
                    'type' => 5,
                    'num' => $model['price'],
                    'foreign_key' => $model['id'],
                    'add_time' => time()
                ])){

                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;

                }

                if(!db('information')->insert([
                    'user_id' => $model['user_id'],
                    'type' => 5,
                    'content' => '购买广告位，支付成功',
                    'add_time' => time()
                ])){

                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }
               
                $user_info = db('user')->where('id',$model['user_id'])->find();

                if(!model('common/admin_bill')->addBill($user_info['from_city'],$model['price'],$model['id'],2)){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }
                
                db()->commit();
                echo $this->arrayToXml(['return_code' => 'SUCCESS','return_msg' => '回调成功']);die;
            }
            
            //购买服务
            if($attach['attach'] == 4){
                //订单详情
                $info = model('common/service_buy')->get($attach['id']);

                //服务详情
                $service_info = db('service')->where('id',$info['foreign_key'])->find();

                db()->startTrans();
                if(!$info->save([
                    'order_no' => $out_trade_no,
                    'tran_no' => $transaction_id,
                    'pay_type' => 1,
                    'pay_time' => $pay_time,
                    'status' => 1
                ])){

                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }
                //售卖者信息
                $service_user = model('common/user')->getUserInfo(['id' => $info['issue_id']],'grade,grade_end_time,from_city');

                //佣金
                if($service_user['grade'] == 1){
                    $yongjin = db('web_config')->where('key','h_yongjin')->value('value');
                }else{
                    $yongjin = db('web_config')->where('key','p_yongjin')->value('value');
                }

                $seller_price = sprintf('%.2f',$info['price'] - $yongjin);  //售卖者所得金额

                if(!db('user')->where('id',$info['issue_id'])->update([
                    'balance' => ['inc',$seller_price],
                    'service_count' => ['inc',1]
                ])){

                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                   
                }

                if(!db('bill')->insertAll([
                    [
                        'user_id' => $info['buy_user'],
                        'type' => 6,
                        'num' => $info['price'],
                        'foreign_key' => $info['id'],
                        'add_time' => time()
                    ],[
                        'user_id' => $info['issue_id'],
                        'type' => 8,
                        'num' => $seller_price,
                        'foreign_key' => $info['id'],
                        'add_time' => time()
                    ]
                ])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                if(!db('information')->insertAll([
                    [
                        'user_id' => $info['buy_user'],
                        'type' => 6,
                        'content' => '服务-'.$service_info['title'].'购买成功',
                        'add_time' => time()
                    ],[
                        'user_id' => $info['issue_id'],
                        'type' => 8,
                        'content' => '服务-'.$service_info['title'].'被购买',
                        'add_time' => time()
                    ]
                ])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                if(!model('common/admin_bill')->addBill($service_user['from_city'],$yongjin,$info['id'],3)){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                db()->commit();

                echo $this->arrayToXml(['return_code' => 'SUCCESS','return_msg' => '回调成功']);die;
            }

            //服务打赏
            if($attach['attach'] == 5){     
                //订单详情
                $model = model('common/service_buy')->get($attach['id']);

                //服务详情
                $service_info = db('service')->where('id',$model['foreign_key'])->find();

                db()->startTrans();

                if(!db('user')->where('id',$model['issue_id'])->setInc('balance',$total_fee)){

                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                if(!db('bill')->insertAll([[
                        'user_id' => $model['buy_user'],
                        'type' => 9,
                        'num' => $total_fee,
                        'foreign_key' => $model['id'],
                        'add_time' => time()
                    ],[
                        'user_id' => $model['issue_id'],
                        'type' => 10,
                        'num' => $total_fee,
                        'foreign_key' => $model['id'],
                        'add_time' => time()
                    ]])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                if(!db('information')->insertAll([[
                        'user_id' => $model['buy_user'],
                        'type' => 9,
                        'content' => '服务-'.$service_info['title'].'打赏成功',
                        'add_time' => time()
                    ],[
                        'user_id' => $model['issue_id'],
                        'type' => 10,
                        'content' => '服务-'.$service_info['title'].'被打赏'.$total_fee.'元',
                        'add_time' => time()
                    ]])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                if(!$model->save([
                    'dashang' => 1,
                    'ds_price' => $total_fee,
                    'ds_pay_type' => 1,
                    'ds_order_no' => $out_trade_no,
                    'ds_tran_no' => $transaction_id,
                    'ds_pay_time' => $pay_time
                ])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                db()->commit();

                echo $this->arrayToXml(['return_code' => 'SUCCESS','return_msg' => '回调成功']);die;

            }

            //会员升级
            if($attach['attach'] == 6){
                //会员详情
                $user_info = model('common/user')->getUserInfo(['id' => $attach['user_id']],'grade,grade_end_time');
                //会员价格详情
                $res = db('grade')->where('id',$attach['type'])->find();
                if($user_info['grade'] == 1){
                    $grade_end_time = strtotime($user_info['grade_end_time']);
                }else{
                    $grade_end_time = strtotime(date('Ymd'));
                }
                switch($attach['type']){
                    case 1: $grade_end_time = strtotime('+1 month',$grade_end_time); break;  //包月
                    case 2: $grade_end_time = strtotime('+6 month',$grade_end_time); break; //半年
                    case 3: $grade_end_time = strtotime('+1 years',$grade_end_time); break; //包年
                }
                db()->startTrans();
                //修改会员信息
                if(!db('user')->where('id',$attach['user_id'])->update([
                    'grade_end_time' => $grade_end_time,
                    'grade' => 1
                ])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }
                $insert = db('upgrade')->insertGetId([
                    'user_id' => $attach['user_id'],
                    'grade_type' => $attach['type'],
                    'price' => $total_fee,
                    'pay_type' => 1,
                    'order_no' => $out_trade_no,
                    'tran_no' => $transaction_id,
                    'pay_time' => $pay_time
                ]);

                //购买记录
                if(!$insert){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }
                if(!db('bill')->insert([
                    'user_id' => $attach['user_id'],
                    'type' => 11,
                    'num' => $total_fee,
                    'foreign_key' => $insert,
                    'add_time' => time()
                ])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                if(!db('information')->insert([
                    'user_id' => $attach['user_id'] ,
                    'type' => 11 ,
                    'content' => '购买会员-支付成功',
                    'add_time' => time()
                ])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }
                db()->commit();
                echo $this->arrayToXml(['return_code' => 'SUCCESS','return_msg' => '回调成功']);die;

            }
            //购买定位
            if($attach['attach'] == 7){
                db()->startTrans();
                //购买记录
                if(!$id = db('position_buy')->insertGetId([
                    'user_id' => $attach['user'],
                    'num' => $attach['num'],
                    'price' => $total_fee,
                    'order_no' => $out_trade_no,
                    'tran_no' => $transaction_id,
                    'pay_type' => 1,
                    'pay_status' => 1,
                    'pay_time' => $pay_time
                ])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }
                
                //添加交易明细
                if(!db('bill')->insert([
                    'user_id' => $attach['user'],
                    'type' => 13,
                    'num' => $total_fee,
                    'foreign_key' => $id,
                    'add_time' => time()
                ])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }

                //消息通知
                if(!db('information')->insert([
                    'user_id' => $attach['user'],
                    'type' => 13,
                    'content' => '购买定位-支付成功',
                    'add_time' => time()
                ])){
                    db()->rollback();
                    echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
                }
                db()->commit();
                echo $this->arrayToXml(['return_code' => 'SUCCESS','return_msg' => '回调成功']);die;
            }
        }
        echo $this->arrayToXml(['return_code' => 'FAIL','return_msg' => '回调失败']);die;
    }


    protected function arrayToXml($arr) {
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            } else
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
        }
        $xml .= "</xml>";
        return $xml;
    }
}
